package org.esiee.mobiliteam;

import java.util.List;
import java.util.Vector;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class Transport extends ListActivity {

	private Integer[] imgid = {
			R.drawable.logorera,R.drawable.logobus212,R.drawable.logobus213,
			R.drawable.logobus312
	};

	protected String[] transports = null;
	private LayoutInflater mInflater;
	private Vector<RowData> data;
	RowData rd;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		transports = getResources().getStringArray(R.array.transports_array);
		mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		data = new Vector<RowData>();
		for(int i=0;i<transports.length;i++){

			try {
				rd = new RowData(i,transports[i]);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			data.add(rd);
		}
		CustomAdapter adapter = new CustomAdapter(this, R.layout.transport_listitem,
				R.id.transport, data);
		setListAdapter(adapter);
		
		ListView lv = getListView();
		lv.setTextFilterEnabled(true);

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// When clicked, show a toast with the TextView text
				if(position == 0){
					Intent intent = new Intent(Transport.this, RATPHandler.class);
					Bundle bundle = new Bundle();
					bundle.putString("url0", "http://www.ratp.fr/horaires/fr/ratp/rer/prochains_passages/RA/Noisy-Champs/A");
					bundle.putString("url1", "http://www.ratp.fr/horaires/fr/ratp/rer/prochains_passages/RA/Noisy-Champs/R");
					bundle.putString("start", "<span class=\"direction\">");
					bundle.putString("end", "</fieldset>");
					intent.putExtras(bundle);
					Transport.this.startActivity(intent);
				}
				else if(position == 1){
					Intent intent = new Intent(Transport.this, RATPHandler.class);
					Bundle bundle = new Bundle();
					bundle.putString("url0", "http://www.ratp.fr/horaires/fr/ratp/bus/prochains_passages/PP/B212/212_97_119/A");
					bundle.putString("url1", "http://www.ratp.fr/horaires/fr/ratp/bus/prochains_passages/PP/B212/212_97_119/R");
					bundle.putString("start", "<div class=\"line_details\">");
					bundle.putString("end", "</fieldset>");
					intent.putExtras(bundle);
					Transport.this.startActivity(intent);
				}
				else if(position == 2){
					Intent intent = new Intent(Transport.this, RATPHandler.class);
					Bundle bundle = new Bundle();
					bundle.putString("url0", "http://www.ratp.fr/horaires/fr/ratp/bus/prochains_passages/PP/B213/213_21_54/A");
					bundle.putString("url1", "http://www.ratp.fr/horaires/fr/ratp/bus/prochains_passages/PP/B213/213_21_54/R");
					bundle.putString("start", "<div class=\"line_details\">");
					bundle.putString("end", "</fieldset>");
					intent.putExtras(bundle);
					Transport.this.startActivity(intent);
				}
				else if(position == 3){
					Intent intent = new Intent(Transport.this, RATPHandler.class);
					Bundle bundle = new Bundle();
					bundle.putString("url0", "http://www.ratp.fr/horaires/fr/ratp/bus/prochains_passages/PP/B312/312_28_29/A");
					bundle.putString("url1", "http://www.ratp.fr/horaires/fr/ratp/bus/prochains_passages/PP/B312/312_28_29/R");
					bundle.putString("start", "<div class=\"line_details\">");
					bundle.putString("end", "</fieldset>");
					intent.putExtras(bundle);
					Transport.this.startActivity(intent);
				}
			}
		});
	}

	private class RowData {
		protected int mId;
		protected String mTitle;
		RowData(int id,String title){
			mId=id;
			mTitle = title;
		}
	}
	private class CustomAdapter extends ArrayAdapter<RowData> {

		public CustomAdapter(Context context, int resource,
				int textViewResourceId, List<RowData> objects) {               

			super(context, resource, textViewResourceId, objects);
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {   

			ViewHolder holder = null;
			TextView title = null;
			ImageView i11=null;
			RowData rowData= getItem(position);

			if(null == convertView){
				convertView = mInflater.inflate(R.layout.transport_listitem, null);
				holder = new ViewHolder(convertView);
				convertView.setTag(holder);
			}
			holder = (ViewHolder) convertView.getTag();
			title = holder.gettitle();
			title.setText(rowData.mTitle);                                                    

			i11=holder.getImage();
			i11.setImageResource(imgid[rowData.mId]);
			return convertView;
		}
		private class ViewHolder {
			private View mRow;
			private TextView title = null;
			private ImageView i11=null; 

			public ViewHolder(View row) {
				mRow = row;
			}
			public TextView gettitle() {
				if(null == title){
					title = (TextView) mRow.findViewById(R.id.transport);
				}
				return title;
			}     


			public ImageView getImage() {
				if(null == i11){
					i11 = (ImageView) mRow.findViewById(R.id.transport_icon);
				}
				return i11;
			}
		}
	} 
}

